<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class ForgottenPassword extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('html');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->library('googlemaps');
        $this->load->helper('url');
        $this->load->model('register_model');
        $this->load->model('users_model');
        $this->load->library('session');
        $this->load->helper('captcha');
    }

    public function index() {



        $vals = array(
            'img_path' => './captcha/',
            'img_url' => base_url() . 'captcha/',
            'img_width' => '240',
            'img_height' => 30,
            'border' => 0,
            'expiration' => 7200
        );


        $cap = create_captcha($vals);


        $data['image'] = $cap['image'];


        $this->session->set_userdata('word', $cap['word']);

        $this->load->view('captcha_view.php', $data);
    }

    public function view() {
        $vals = array(
            'img_path' => './captcha/',
            'img_url' => base_url() . 'captcha/',
            'img_width' => '267',
            'img_height' => 30,
            'border' => 0,
            'expiration' => 7200
        );


        $cap = create_captcha($vals);


        $data['image'] = $cap['image'];


        $this->session->set_userdata('word', $cap['word']);
        echo json_encode($data);
    }

    public function sendEmail() {
        if ($this->_submit_validate_email() === FALSE) {
            echo json_encode("invalid_email");
        } else {
            if ($this->input->post() && ($this->input->post('word') == $this->session->userdata('word'))) {
                $config = Array(
                    'protocol' => "smtp",
                    'smtp_host' => "ssl://smtp.googlemail.com",
                    'smtp_port' => 465,
                    'smtp_user' => "soferujem@gmail.com",
                    'smtp_pass' => "adampeto"
                );

                $to_ = $this->input->post('email');

                $message = "aloha";
                $hash = md5(sha1($to_));
                $page = base_url() . "profile/newPass/" . $hash;



                $this->users_model->updateHash($hash, $to_);

                echo json_encode("correct_captcha");
                $this->load->library('email', $config);
                $this->email->set_newline("\r\n");
                $this->email->from('soferujem@gmail.com', 'Soferujem');
                $this->email->to($to_);
                $this->email->subject('Zabudnuté heslo');
                $this->email->message("stranka je: " . $page);
                $this->email->send();
                // $this->load->view('success_view.php');
            } else {
                echo json_encode("wrong_captcha");
            }
        }
    }

    private function _submit_validate_email() {

        $this->form_validation->set_rules('email', 'E-mail', 'required|valid_email');
        return $this->form_validation->run();
    }

}